﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Controls;
using Model;

namespace TanHungLong
{
    public partial class FrmThietDat : RmsControl.FrmBase
    {
        #region variant
        private FrmMain main = null;
        private FormStatus formStatus = FormStatus.View;
        #endregion variant

        //=====================================
        #region form
        public FrmThietDat(FrmMain main)
        {
            InitializeComponent();

            this.main = main;
            this.main.ShowBgr();
            ShowForm(this.main);

            this.Load += new EventHandler(FrmThietDat_Load);
            this.FormClosing += new FormClosingEventHandler(FrmThietDat_FormClosing);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnEdit.Click += new EventHandler(btnEdit_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            SetFormStatus(this.formStatus);
        }

        private void FrmThietDat_Load(object sender, EventArgs e)
        {
            RefreshData();
            SetFormStatus(this.formStatus);
        }

        private void FrmThietDat_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
            else
            {
                this.main.Controls.Remove(this);
                this.main.HideBgr();
                this.Dispose();
            }
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            Session.thietDatBOCollection = ThietDatBOCollection.GetThietDatBOCollection();

            this.txtDiaChi.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.DiaChi).NoiDungS;
            this.txtDienThoai.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.DienThoai).NoiDungS;
            this.txtFax.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.Fax).NoiDungS;
            this.txtWebsite.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.Website).NoiDungS;
            this.txtEmail.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.Email).NoiDungS;
            this.txtMaSoThue.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.MaSoThue).NoiDungS;
            this.txtGiayPhepKinhDoanh.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.GiayPhepKinhDoanh).NoiDungS;
            this.txtSoTaiKhoan.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.SoTaiKhoan).NoiDungS;
            this.txtNganHang.Text = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.NganHang).NoiDungS;
            this.numLuongToiThieu.Value = (decimal)StaticUtil.GetDouble(Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.LuongToiThieu).NoiDungF, 0);
            this.picAnh.EditValue = Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.Anh).NoiDungI;
            this.numBHYT.Value = (decimal)StaticUtil.GetDouble(Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.BHYT).NoiDungF, 0);
            this.numBHXH.Value = (decimal)StaticUtil.GetDouble(Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.BHXH).NoiDungF, 0);
            this.numBHTN.Value = (decimal)StaticUtil.GetDouble(Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.BHTN).NoiDungF, 0);
            this.numNgayTinhLuong.Value = (decimal)StaticUtil.GetDouble(Session.thietDatBOCollection.GetThietDatBO(LoaiThietDat.NgayTinhLuong).NoiDungF, 0);
        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void Save()
        {
            if (this.txtDiaChi.Focused == false)
                this.txtDiaChi.Focus();
            else
                this.txtDienThoai.Focus();

            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.DiaChi, this.txtDiaChi.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.DienThoai, this.txtDienThoai.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.Fax, this.txtFax.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.Website, this.txtWebsite.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.Email, this.txtEmail.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.MaSoThue, this.txtMaSoThue.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.GiayPhepKinhDoanh, this.txtGiayPhepKinhDoanh.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.SoTaiKhoan, this.txtSoTaiKhoan.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.NganHang, this.txtNganHang.Text);
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.LuongToiThieu, decimal.ToDouble(this.numLuongToiThieu.Value));
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.Anh, this.picAnh.EditValue);

            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.BHYT, decimal.ToDouble(this.numBHYT.Value));
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.BHXH, decimal.ToDouble(this.numBHXH.Value));
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.BHTN, decimal.ToDouble(this.numBHTN.Value));
            Session.thietDatBOCollection.SetThietDatBO(LoaiThietDat.NgayTinhLuong, decimal.ToDouble(this.numNgayTinhLuong.Value));

            Session.thietDatBOCollection = Session.thietDatBOCollection.Save();

            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;

            RefreshData();
        }

        private void SetFormStatus(FormStatus formStatus)
        {
            bool edit = (formStatus != FormStatus.View);
            this.allowCloseForm = !edit;

            Utility.SetControlReadOnly(this.pnMain, !edit);

            this.btnEdit.Visible = !edit;
            this.btnClose.Visible = !edit;
            this.btnRefresh.Visible = !edit;

            this.btnSave.Visible = edit;
            this.btnCancel.Visible = edit;
        }
        #endregion function
    }
}